package com.mengdie.shuidi.ui.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.mengdie.shuidi.AppContext;
import com.mengdie.shuidi.OkHeader;
import com.mengdie.shuidi.R;
import com.mengdie.shuidi.api.network.StringDialogCallback;
import com.mengdie.shuidi.manager.MDAccountMgr;
import com.mengdie.shuidi.model.BaseModel;
import com.mengdie.shuidi.utils.ToastUtil;
import com.mengdie.shuidi.utils.common.RegularUtils;
import com.mengdie.shuidi.widgets.ButtonTimer;

import org.json.JSONObject;

import java.util.HashMap;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by apple on 16/10/13.
 */

public class EmailActivity extends BaseActivity {

    @Bind(R.id.rl_generic_back)
    RelativeLayout mRlGenericBack;
    @Bind(R.id.tv_generic_title)
    TextView mTvGenericTitle;
    @Bind(R.id.et_bind_email)
    EditText mEtBindEmail;
    @Bind(R.id.bt_send_code)
    ButtonTimer mBtSendCode;
    @Bind(R.id.et_email_code)
    EditText mEtEmailCode;
    @Bind(R.id.tv_email_submit)
    TextView mTvEmailSubmit;

    private String email = "";
    private String code;
    private String title;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_email);
        ButterKnife.bind(this);
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            email = bundle.getString("email");
        }
        init();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        OkGo.getInstance().cancelTag(this);
    }

    public void init() {
        if (!TextUtils.isEmpty(email)) {
            title = "更换邮箱";
        } else {
            title = "绑定邮箱";
        }
        mTvGenericTitle.setText("绑定邮箱");
        mEtBindEmail.setText(email);
    }

    @OnClick({R.id.rl_generic_back, R.id.bt_send_code, R.id.tv_email_submit})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rl_generic_back:
                onBackPressed();
                break;
            case R.id.bt_send_code:
                email = mEtBindEmail.getText().toString();
                if (!TextUtils.isEmpty(email)) {
                    if (RegularUtils.isEmail(email)) {
                        getCode(email);
                    } else {
                        ToastUtil.showToast("邮箱格式不正确!");
                    }
                } else {
                    ToastUtil.showToast("邮箱不能为空!");
                }
                break;
            case R.id.tv_email_submit:
                email = mEtBindEmail.getText().toString();
                code = mEtEmailCode.getText().toString();
                if (!TextUtils.isEmpty(email)) {
                    if (!TextUtils.isEmpty(code)) {
                        if (RegularUtils.isEmail(email)) {
                            setEmail(email, code);
                        } else {
                            ToastUtil.showToast("邮箱格式不正确!");
                        }

                    } else {
                        ToastUtil.showToast("验证码不能为空!");
                    }
                } else {
                    ToastUtil.showToast("邮箱不能为空!");
                }


                break;
        }
    }


    public void setEmail(String email, String code) {
        HashMap map = new HashMap();
        map.put("session_id", MDAccountMgr.getInstance().getSessionId());
        map.put("email", email);
        map.put("email_code", code);
        JSONObject json = new JSONObject(map);


        OkGo.getInstance().addCommonHeaders(OkHeader.getCommonHeadToken(this)).post(AppContext.getInstance().getBaseUrl()+"account/set_email")
                .tag(EmailActivity.this)
                .upJson(json.toString())
                .execute(new StringDialogCallback(EmailActivity.this, "加载中...", true) {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        Gson gson = new Gson();
                        BaseModel baseModel = gson.fromJson(s, BaseModel.class);
                        if (baseModel.getRet().equals("0")) {
                            finish();
                            ToastUtil.showToast(baseModel.getMsg());
                        } else {
                            ToastUtil.showToast(baseModel.getMsg());
                        }
                    }
                });

    }

    public void getCode(String email) {
        HashMap<String, String> map = new HashMap<>();
        map.put("num", email);
        JSONObject json = new JSONObject(map);

        OkGo.getInstance().addCommonHeaders(OkHeader.getCommonHeadToken(this)).post(AppContext.getInstance().getBaseUrl()+"account/get_code")
                .tag(EmailActivity.this)
                .upJson(json.toString())
                .execute(new StringDialogCallback(EmailActivity.this, "加载中...", true) {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        Gson gson = new Gson();
                        BaseModel baseModel = gson.fromJson(s, BaseModel.class);
                        if (baseModel.getRet().equals("0")) {
                            mBtSendCode.setTimes(60);
                            if (!mBtSendCode.isRun()) {
                                mBtSendCode.beginRun();
                                mBtSendCode.setEnabled(false);
                            }
                            ToastUtil.showToast(baseModel.getMsg());
                        } else {
                            ToastUtil.showToast(baseModel.getMsg());
                        }
                    }
                });

    }
}
